package com.tinyideatech.admin.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.MallMember;
import com.tinyideatech.system.mapper.MallOrderMapper;
import com.tinyideatech.system.service.IMallMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-08-29
 */

@Controller
@RequestMapping("/mall/member")
public class MallMemberController extends BaseController {

    @Autowired
    private IMallMemberService mallMemberService;

    @Autowired
    private MallOrderMapper mallOrderMapper;

    @GetMapping()
    public String list() {
        return "mall/member/list";
    }

    @GetMapping("/add")
    public String add() {
        return "mall/member/add";
    }

    @GetMapping("/edit/{id}")
    public String mallMemberEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        MallMember mallMember = mallMemberService.getById(id);
        map.addAttribute("mallMember", mallMember);
        return "mall/member/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(MallMember mallMember) {
        QueryWrapper<MallMember> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(mallMember.getNickname())) {
            qw.like("nickname", mallMember.getNickname());
        }
        if (StringUtils.isNotEmpty(mallMember.getDeviceId())) {
            qw.like("device_id", mallMember.getDeviceId());
        }
        if (StringUtils.isNotEmpty(mallMember.getOpenId())) {
            qw.like("open_id", mallMember.getOpenId());
        }
        IPage<MallMember> page = startPage();
        IPage<MallMember> mallMemberList = mallMemberService.page(page, qw);
        return page(mallMemberList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            MallMember sysRole
    ) {
        mallMemberService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ) {
        Long[] mallMemberIds = Convert.toLongArray(ids);
        mallMemberService.removeByIds(Arrays.asList(mallMemberIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(MallMember mallMember) {
        mallMemberService.changeStatus(mallMember);
        return success();
    }


    @PostMapping("/deviceId")
    @ResponseBody
    public ApiResult getDeviceId(@RequestParam long id) {
        String dId = mallMemberService.getOne(new QueryWrapper<MallMember>().eq("member_id", id)).getDeviceId();
        return success(dId);
    }

    @PostMapping("/sum")
    @ResponseBody
    public ApiResult getSum(@RequestParam long id) {
        return success(mallOrderMapper.consumeById(id));
    }


}
